﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.DAL;
using WXYW.Entities;

namespace WXYW.BLL
{
    public class ProductService : RepositoryBase
    {
        public List<Product> GetProductsByKeyword(string keyword)
        {
            List<Product> products = new List<Product>();
            if (string.IsNullOrEmpty(keyword))
            {
                products = GetEntities<Product>().OrderBy(p=>p.ProductClass.Name).ToList();
            }
            else
            {
                products = GetEntities<Product>(p => p.MaterialID.Contains(keyword) || p.ModelNumber.Contains(keyword)
                    || p.ProductClass.Name.Contains(keyword) || p.VersionNumber.Contains(keyword)
                    || p.Property.Contains(keyword)).OrderBy(p => p.ProductClass.Name).ToList();
            }

            foreach (Product product in products)
            {
                if (!product.ProductClassReference.IsLoaded)
                {
                    product.ProductClassReference.Load();
                }
                if (!product.ProviderReference.IsLoaded)
                {
                    product.ProviderReference.Load();
                }
            }
            return products;
        }

        public List<Product> GetAllProducts()
        {
            IQueryable<Product> products = GetEntities<Product>();
            foreach (Product product in products)
            {
                if (!product.ProductClassReference.IsLoaded)
                {
                    product.ProductClassReference.Load();
                }
                if (!product.ProviderReference.IsLoaded)
                {
                    product.ProviderReference.Load();
                }
            }
            return products.ToList();
        }

        public void AddProduct(Product newProduct)
        {
            InsertEntity(newProduct);
        }

        public void UpdateProduct(int WLID, string productNO, int typeID, string description, int repairPeriod, long providerID, string property, int updateBy)
        {
            //Product product = GetEntities<Product>().Where(p => p.WLID == WLID).FirstOrDefault();

            //product.ProductNO = productNO;
            //product.ProductType = GetEntities<ProductType>().Where(t => t.ProductTypeID == typeID).FirstOrDefault();
            //product.Property = property;
            //product.Provider = GetEntities<Provider>().Where(p => p.ProviderID == providerID).FirstOrDefault();
            //product.RepairPeriod = repairPeriod;
            //product.UpdateBy = updateBy;
            //product.UpdateDateTime = DateTime.Now;

            //UpdateEntity(product);
        }

        public void Update(Product product)
        {
            UpdateEntity(product);
        }

        public void DeleteProduct(int wlID)
        {
            //DeleteEntity<Product>(p=>p.WLID == wlID, Guid.NewGuid());
        }

        public void Delete(int productid)
        {
            DeleteEntity<Product>(p => p.ProductID == productid, Guid.Empty);
        }

        public ApplicationProduct GetProductByID(long guid)
        {
            return GetEntities<ApplicationProduct>(p => p.ApplicationProductID == guid).FirstOrDefault();
        }

        public Product GetProductByProductID(long id)
        {
            return GetEntities<Product>(p => p.ProductID == id).FirstOrDefault();
        }

        public void AddProduct(string materialNO, string modelNO, string versionNO, string description, int providerID, int productClassID,
            int repairPeriod, string property, User currentOperator)
        {
            Product newProdutEntity = new Product();
            newProdutEntity.Description = description;
            newProdutEntity.MaterialID = materialNO;
            newProdutEntity.ModelNumber = modelNO;
            newProdutEntity.ProductClass = GetEntities<ProductClass>().Where(p => p.ID == productClassID).FirstOrDefault();
            newProdutEntity.Property = property;
            newProdutEntity.Provider = GetEntities<Provider>().Where(p => p.ProviderID == providerID).FirstOrDefault();
            newProdutEntity.RepairPeriod = repairPeriod;
            newProdutEntity.UpdateDateTime = DateTime.Now;
            newProdutEntity.UpdateBy = currentOperator.UserID;
            newProdutEntity.VersionNumber = versionNO;
            InsertEntity(newProdutEntity);

            ProductType productType = GetEntities<ProductType>().Where(p => p.ProductTypeName.Equals(modelNO)).FirstOrDefault();
            if (productType == null)
            {
                productType = new ProductType();
                productType.ProductTypeName = modelNO;
                productType.ProductClass = GetEntities<ProductClass>().Where(p => p.ID == productClassID).FirstOrDefault();
                InsertEntity(productType);
            }
        }

        internal void UpdateProduct(ApplicationProduct product)
        {
            UpdateEntity(product);
        }

        public ProductInfo GetProductInfoBySerialNO(string serialNO)
        {
            return GetEntities<ProductInfo>(p => p.SerialNO == serialNO).FirstOrDefault();
        }

        public IQueryable<ProductInfo> GetAllProductInfo(string keyword)
        {
            if (string.IsNullOrEmpty(keyword))
            {
                return GetEntities<ProductInfo>().OrderBy(p => p.ModelNO);
            }
            else
            {
                return GetEntities<ProductInfo>().Where(p=>p.ModelNO.Contains(keyword) || p.JXSName.Contains(keyword) || p.SerialNO.Contains(keyword) || p.ZZUser.Contains(keyword)).OrderBy(p=>p.ModelNO);
            }
        }

        public List<Product> GetProductByProductClassID(int productClassID)
        {
            return GetEntities<Product>().Where(p => p.ProductClass.ID == productClassID).ToList();
        }

        public void CalculateRepairTimes()
        {
            List<Product> products = GetEntities<Product>().ToList();
            foreach (Product product in products)
            {
                ProductType productType = GetEntities<ProductType>().Where(p => p.ProductTypeName.Equals(product.ModelNumber)).FirstOrDefault();
                if (productType != null)
                {
                    product.RepairTimes = GetEntities<ApplicationProduct>().Where(a => a.ProductType.ProductTypeID == productType.ProductTypeID && !string.IsNullOrEmpty(a.ApplicationForm.ApplicationCode)).Count();                        
                }
                if (product.RepairTimes == null)
                {
                    product.RepairTimes = 0;
                }
                UpdateEntity(product);
            }
        }
    }
}
